using System;
using Impact.Controllers;
using Impact.Model.Objects;
using System.IO;
using System.Web;
using System.Web.UI.WebControls;
using Impact.Web.Foundation.Controls;

namespace Impact.Web.Admin
{
    public partial class Photos : Impact.Web.Foundation.PageBase
    {

        /// <summary>
        /// loading event handler
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad( EventArgs e )
        {
            base.OnLoad( e );

            if ( Request.Form["__EVENTTARGET"] == "Bind" )
            {
                BindPhotos();
            }
        }

        /// <summary>
        /// data binding specific to this page
        /// </summary>
        protected override void Bind()
        {
            base.Bind();

            galleries.DataSource = PhotoController.GetAllGalleries();
            galleries.DataTextField = PhotoGallery.Columns.Name;
            galleries.DataValueField = PhotoGallery.Columns.GalleryID;
            galleries.DataBind();

            if ( !string.IsNullOrEmpty( Request.QueryString["id"] ) )
            {
                galleries.SelectedValue = Request.QueryString["id"];
            }

            BindPhotos();
        }

        private void BindPhotos()
        {
            PhotoController.UpdateGallery( galleries.SelectedValue );

            PhotoGallery selected = PhotoController.GetGallery( galleries.SelectedValue );
            if ( selected.Photos.Count > 0 )
            {
                photoList.Visible = true;
                photoList.DataSource = selected.Photos;
                photoList.DataBind();
                noPhotosPanel.Visible = false;
            }
            else
            {
                photoList.Visible = false;
                noPhotosPanel.Visible = true;
            }
        }

        /// <summary>
        /// event handler executed when the user changes the active gallery
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GalleryChanged( object sender, EventArgs e )
        {
            BindPhotos();
        }

        /// <summary>
        /// retrieves the HTML to display the given item in the browser
        /// </summary>
        /// <param name="item">a Photo object to get HTML for</param>
        protected string GetHtmlImage( object item )
        {
            Photo photo = item as Photo;
            if ( photo != null )
            {
                string thumbPath = ResolveClientUrl( PhotoController.GetThumbnailPath( photo ) );

                return string.Format( "<a href=\"EditPhoto.aspx?id={0}\"><img src=\"{1}\" alt=\"{2}\" border=\"0\" /></a>",
                    photo.PhotoID, thumbPath, photo.Caption );
            }
            return string.Empty;
        }

        /// <summary>
        /// event handler executes when the user clicks a button in the PhotoList Repeater
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void PhotoList_ItemCommand( object source, DataListCommandEventArgs e )
        {
            switch ( e.CommandName )
            {
                case "delete":
                    try
                    {
                        PhotoController.DeletePhoto( e.CommandArgument.ToString() );
                        MessageBox.Show( "Photo Deleted" );
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show( string.Format( "Photo could not be deleted: {0}", ex.Message ) );
                    }
                    break;
                default:
                    throw new ArgumentException( "Unknown command" );
            }
            BindPhotos();
        }

    }
}
